package net.mikoo.seals.boss.common;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SpecialDateEditor extends PropertyEditorSupport {

    private final Logger logger = LoggerFactory.getLogger(getClass());  

    @Override  
    public void setAsText(String text) throws IllegalArgumentException {  
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        Date date = null;  
        try {  
            //防止空数据出错  
            if(StringUtils.isNotBlank(text)){  
                date = format.parse(text);  
            }  
        } catch (ParseException e) {  
            format = new SimpleDateFormat("yyyy-MM-dd");  
            try {  
                date = format.parse(text);  
            } catch (ParseException e1) {  
                format = new SimpleDateFormat("yyyy-MM");  

                try{  
                    date = format.parse(text);  
                }catch (Exception e2) {  
                    logger.error("自动绑定日期数据出错", e);  
                }  
            }  
        }  
        setValue(date);  
    }  
}
